//  program:	Minority Reports Permutation Models
//  task:       Produce permutation results from published JOP paper appendix
//  project:    Minority Reports
//  author:     Josh Ryan

// #Start
// program setup
// Notes:		The models are found in the online appendix of the paper. Because the models are simulations,
//				exact results will not be reproduced. The work below includes the loop file used to produce the 
//				the permutations, along with the graphic commands, etc.

version 11.1
clear all 
set linesize 80
macro drop _all
set seed 123456
set scheme s2mono


*Call the data:
use "04workingrollcallandconferencedata041913.dta", clear

MODEL 1
*Permute within votes, by randomizing votes then combining and estimating
*Permutation of Table 7, Pooled votes
use "04workingrollcallandconferencedata041913.dta", clear

reg krehbielwc minstate majoritysize2 chamdiff legprof_squire chambersize /// 
perconfvote numtotvote i.statenu, cluster(statecham) /*base model */


use "04workingrollcallandconferencedata041913.dta", clear
set more off
*set number of iterations
*loop starts here with {
forval x=1/1000 {
*display iterations
disp = "Iteration `x'"
*shh, iterate quietly please
qui {
*I told it to open the data at the end of the loop rather than the  beginning
*it kept crashing and I'm hoping this works better
*open the data at the end, keep time and votes
*use "allvotes050512.dta", clear
*drop all variables except time counter, start time, v0m and the votes, v*
*Note that each vote has a dummy, 1=0 that vote
keep statecham _j minstate
*sort by vote, then generate a random number within each vote number
sort statecham _j
gen random = uniform()
*now sort by vote,  then by random number so that within each vote,
*time, t1 is randomly sorted
sort random
*generate a counter n, that is an observation identifier so that obs 1
*has a random t1
gen rancount=_n
*now just t1 trandom to keep it separate from the actual t1 in the dataset
rename statecham ranstatecham
rename _j ran_j
rename minstate ranminstate
drop random
*now I've got a randomly generated list of time by vote
save "random.dta",replace
*call the normal data again
use "04workingrollcallandconferencedata041913.dta", clear

*merge data
*then generate a counter
sort statecham _j
gen rancount=_n
*now merge using each vote and its individual counter identifier with randomly sorted data
merge 1:1 rancount using "random.dta"
drop _merge

*note that cluserting is taken out of these, that's kind of the whole point
reg krehbielwc ranminstate majoritysize2 chamdiff legprof_squire chambersize /// 
perconfvote numtotvote i.statenu /*base model */

gen b_ranminstate=_b[ranminstate]
gen se_ranminstate=_se[ranminstate]

keep in 1
keep b_* se_*
save "tempperm.dta", replace
clear
use  "permcoeffsmodel1.dta", clear
append using "tempperm.dta"
save "permcoeffsmodel1.dta", replace
clear
use "04workingrollcallandconferencedata041913.dta", clear
}
}
***********************






use "04workingrollcallandconferencedata041913.dta", clear
set more off
*set number of iterations
*loop starts here with {
forval x=1/1000 {
*display iterations
disp = "Iteration `x'"
*shh, iterate quietly please
qui {
*I told it to open the data at the end of the loop rather than the  beginning
*it kept crashing and I'm hoping this works better
*open the data at the end, keep time and votes
*use "allvotes050512.dta", clear
*drop all variables except time counter, start time, v0m and the votes, v*
*Note that each vote has a dummy, 1=0 that vote
keep statecham _j minstate chamdiff
*sort by vote, then generate a random number within each vote number
sort statecham _j
gen random = uniform()
*now sort by vote,  then by random number so that within each vote,
*time, t1 is randomly sorted
sort random
*generate a counter n, that is an observation identifier so that obs 1
*has a random t1
gen rancount=_n
*now just t1 trandom to keep it separate from the actual t1 in the dataset
rename statecham ranstatecham
rename _j ran_j
rename minstate ranminstate
drop random
*now I've got a randomly generated list of time by vote
save "random.dta",replace
*call the normal data again
use "04workingrollcallandconferencedata041913.dta", clear

keep statecham _j chamdiff
*sort by vote, then generate a random number within each vote number
sort statecham _j
gen random = uniform()
*now sort by vote,  then by random number so that within each vote,
*time, t1 is randomly sorted
sort random
*generate a counter n, that is an observation identifier so that obs 1
*has a random t1
gen rancount=_n
*now just t1 trandom to keep it separate from the actual t1 in the dataset
rename chamdiff ranchamdiff
drop statecham _j random
*now I've got a randomly generated list of time by vote
save "random2.dta",replace
*call the normal data again
use "04workingrollcallandconferencedata041913.dta", clear

*merge data
*then generate a counter
sort statecham _j
gen rancount=_n
*now merge using each vote and its individual counter identifier with randomly sorted data
merge 1:1 rancount using "random.dta"
drop _merge
merge 1:1 rancount using "random2.dta"
drop _merge

*note that cluserting is taken out of these, that's kind of the whole point
gen minchamdiff=ranminstate*ranchamdiff
reg krehbielwc ranminstate minchamdiff ranchamdiff  majoritysize2 c.legprof_squire chambersize ///
perconfvote numtotvote i.statenu

gen b_ranminstate=_b[ranminstate]
gen se_ranminstate=_se[ranminstate]

gen b_minchamdiff=_b[minchamdiff]
gen se_minchamdiff=_se[minchamdiff]

gen b_ranchamdiff=_b[ranchamdiff]
gen se_ranchamdiff=_se[ranchamdiff]

keep in 1
keep b_* se_*
save "tempperm2.dta", replace
clear
use  "permcoeffsmodel2.dta", clear
append using "tempperm2.dta"
save "permcoeffsmodel2.dta", replace
clear
use "04workingrollcallandconferencedata041913.dta", clear
}
}
***********************



use "04workingrollcallandconferencedata041913.dta", clear
set more off
*set number of iterations
*loop starts here with {
forval x=1/1000 {
*display iterations
disp = "Iteration `x'"
*shh, iterate quietly please
qui {
*I told it to open the data at the end of the loop rather than the  beginning
*it kept crashing and I'm hoping this works better
*open the data at the end, keep time and votes
*use "allvotes050512.dta", clear
*drop all variables except time counter, start time, v0m and the votes, v*
*Note that each vote has a dummy, 1=0 that vote
keep statecham _j minstate chamdiff
*sort by vote, then generate a random number within each vote number
sort statecham _j
gen random = uniform()
*now sort by vote,  then by random number so that within each vote,
*time, t1 is randomly sorted
sort random
*generate a counter n, that is an observation identifier so that obs 1
*has a random t1
gen rancount=_n
*now just t1 trandom to keep it separate from the actual t1 in the dataset
rename statecham ranstatecham
rename _j ran_j
rename minstate ranminstate
drop random
*now I've got a randomly generated list of time by vote
save "random.dta",replace
*call the normal data again
use "04workingrollcallandconferencedata041913.dta", clear

keep statecham _j chamdiff
*sort by vote, then generate a random number within each vote number
sort statecham _j
gen random = uniform()
*now sort by vote,  then by random number so that within each vote,
*time, t1 is randomly sorted
sort random
*generate a counter n, that is an observation identifier so that obs 1
*has a random t1
gen rancount=_n
*now just t1 trandom to keep it separate from the actual t1 in the dataset
rename chamdiff ranchamdiff
drop statecham _j random
*now I've got a randomly generated list of time by vote
save "random2.dta",replace
*call the normal data again
use "04workingrollcallandconferencedata041913.dta", clear

*merge data
*then generate a counter
sort statecham _j
gen rancount=_n
*now merge using each vote and its individual counter identifier with randomly sorted data
merge 1:1 rancount using "random.dta"
drop _merge
merge 1:1 rancount using "random2.dta"
drop _merge

*note that cluserting is taken out of these, that's kind of the whole point
gen minchamdiff=ranminstate*ranchamdiff
reg krehbielwc ranminstate minchamdiff ranchamdiff  majoritysize2 c.legprof_squire chambersize ///
perconfvote numtotvote i.statenu if unifleg==1

gen b_ranminstate=_b[ranminstate]
gen se_ranminstate=_se[ranminstate]

gen b_minchamdiff=_b[minchamdiff]
gen se_minchamdiff=_se[minchamdiff]

gen b_ranchamdiff=_b[ranchamdiff]
gen se_ranchamdiff=_se[ranchamdiff]

keep in 1
keep b_* se_*
save "tempperm3.dta", replace
clear
use  "permcoeffsmodel3.dta", clear
append using "tempperm3.dta"
save "permcoeffsmodel3.dta", replace
clear
use "04workingrollcallandconferencedata041913.dta", clear
}
}
***********************

use "04workingrollcallandconferencedata041913.dta", clear
set more off
*set number of iterations
*loop starts here with {
forval x=1/1000 {
*display iterations
disp = "Iteration `x'"
*shh, iterate quietly please
qui {
*I told it to open the data at the end of the loop rather than the  beginning
*it kept crashing and I'm hoping this works better
*open the data at the end, keep time and votes
*use "allvotes050512.dta", clear
*drop all variables except time counter, start time, v0m and the votes, v*
*Note that each vote has a dummy, 1=0 that vote
keep statecham _j minstate
*sort by vote, then generate a random number within each vote number
sort statecham _j
gen random = uniform()
*now sort by vote,  then by random number so that within each vote,
*time, t1 is randomly sorted
sort random
*generate a counter n, that is an observation identifier so that obs 1
*has a random t1
gen rancount=_n
*now just t1 trandom to keep it separate from the actual t1 in the dataset
rename statecham ranstatecham
rename _j ran_j
rename minstate ranminstate
drop random
*now I've got a randomly generated list of time by vote
save "random.dta",replace
*call the normal data again
use "04workingrollcallandconferencedata041913.dta", clear

*merge data
*then generate a counter
sort statecham _j
gen rancount=_n
*now merge using each vote and its individual counter identifier with randomly sorted data
merge 1:1 rancount using "random.dta"
drop _merge

*note that cluserting is taken out of these, that's kind of the whole point
logit losingvote ranminstate majoritysize2 chamdiff legprof_squire chambersize /// 
perconfvote numtotvote i.statenu /*base model */

gen b_ranminstate=_b[ranminstate]
gen se_ranminstate=_se[ranminstate]

keep in 1
keep b_* se_*
save "tempperm4.dta", replace
clear
use  "permcoeffsmodel4.dta", clear
append using "tempperm4.dta"
save "permcoeffsmodel4.dta", replace
clear
use "04workingrollcallandconferencedata041913.dta", clear
}
}
***********************


*model 1, using "permcoeffsmodel1.dta" 
graph drop _all
use permcoeffsmodel1.dta, replace

gen minstatez=b_ranminstate/se_ranminstate 
label variable minstatez "minstate z-score"

centile minstatez, centile(5 95)

kdensity minstatez, title("Minority Report (Model 1)") xtitle("T-Score Distribution") ytitle("Density" " ")  graphregion(fcolor(white) ifcolor(white)) scheme(s1mono) ///
xline(-1.678, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(1.6, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(3.14) name(kb1f)

*************************model 2 using "permcoeffsmodel2.dta
use permcoeffsmodel2.dta, replace
*graph drop _all

gen minstatez=b_ranminstate/se_ranminstate 
label variable minstatez "minstate z-score"

gen minchamdiffz=b_minchamdiff/se_minchamdiff 
label variable minchamdiffz "minchamdiff z-score"

gen chamdiffz=b_ranchamdiff/se_ranchamdiff 
label variable chamdiffz "chamdiff z-score"

centile minstatez, centile(5 95)
kdensity minstatez, title("Minority Report (Model 2)" ) xtitle("T-Score Distribution") ytitle("Density" " ")  graphregion(fcolor(white) ifcolor(white)) scheme(s1mono) ///
xline(-1.748, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(1.647, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(2.87) name(kb2f)

centile minchamdiffz, centile(5 95)
kdensity minchamdiffz, title("Minority Report*Cham. Diff. (Model 2)") xtitle("T-Score Distribution") ytitle("Density" " ")  graphregion(fcolor(white) ifcolor(white)) scheme(s1mono) ///
xline(-1.77, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(1.642, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(-2.75) name(kb3f)

centile chamdiffz, centile(5 95)
kdensity chamdiffz, title("Chamber Differences (Model 2)") xtitle("T-Score Distribution") ytitle("Density" " ")  graphregion(fcolor(white) ifcolor(white)) scheme(s1mono) ///
xline(-1.791, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(1.622, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(3.5625) name(kb4f)



*************************model 3 using "permcoeffsmodel3.dta
use permcoeffsmodel3.dta, replace
*graph drop _all

gen minstatez=b_ranminstate/se_ranminstate 
label variable minstatez "minstate z-score"

gen minchamdiffz=b_minchamdiff/se_minchamdiff 
label variable minchamdiffz "minchamdiff z-score"

gen chamdiffz=b_ranchamdiff/se_ranchamdiff 
label variable chamdiffz "chamdiff z-score"

centile minstatez, centile(5 95)
kdensity minstatez, title("Minority Report (Model 3)") xtitle("T-Score Distribution") ytitle("Density" " ")  graphregion(fcolor(white) ifcolor(white)) scheme(s1mono) ///
xline(-1.589, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(1.7344, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(2.085) name(kb5f)

centile minchamdiffz, centile(5 95)
kdensity minchamdiffz, title("Minority Report*Cham. Diff. (Model 3)") xtitle("T-Score Distribution") ytitle("Density" " ")  graphregion(fcolor(white) ifcolor(white)) scheme(s1mono) ///
xline(-1.7628, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(1.469, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(2.075) name(kb6f)

centile chamdiffz, centile(5 95)
kdensity chamdiffz, title("Chamber Differences (Model 3)") xtitle("T-Score Distribution") ytitle("Density" " ")  graphregion(fcolor(white) ifcolor(white)) scheme(s1mono) ///
xline(-1.609, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(1.599, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(-1.54) name(kb7f)

graph combine kb1f kb2f kb3f kb4f kb5f kb6f kb7f,  col(2) scale(.4) graphregion(fcolor(white)) scheme(s2mono)


***********************model using "permcoeffsmodel4.dta

*model 1, using "permcoeffsmodel1.dta" 
*graph drop _all
use permcoeffsmodel4.dta, replace

gen minstatez=b_ranminstate/se_ranminstate 
label variable minstatez "minstate z-score"

centile minstatez, centile(5 95)

kdensity minstatez, title("Minority Report (Logit Model)") xtitle("Z-Score Distribution") ytitle("Density" " ")  graphregion(fcolor(white) ifcolor(white)) scheme(s1mono) ///
xline(-1.544, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(1.822, lpattern(dot) lwidth(medthick) lcolor(gray)) xline(2.144) name(kb8f)


graph combine kb1f kb2f kb3f kb4f kb5f kb6f kb7f kb8f,  col(2) scale(.4) graphregion(fcolor(white)) scheme(s2mono)

